﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tradex.ComponentModel;
using Tradex.Messages;

namespace Tradex.Connectivity {

    /// <summary>
    /// The Connection object stores data about a connection. This data is split into different
    /// datablocks:
    /// * Info contains static information about the setup of the connection.
    /// * Update contains regular information about the state of the connection in terms of data flow counters
    /// * Status contains the status of the connection as per last report.
    /// This data is constructed from ConnectionUpdateMessage instances that are made available to the object.
    /// </summary>
    [Serializable ()]
    public sealed class Connection : DynamicObject {

        /// <summary>
        /// Public Constructor taking the key.
        /// </summary>
        /// <param name="key"></param>
        public Connection (
            ConnectionKey key
        ) {
            Key = key;
        }

        /// <inheritdoc />
        public override void ProcessMessage(Message message) {
            if (message == null) {
                return;
            }
            switch (message.Code) {
                case MessageCode.ConnectionUpdate:
                    ConnectionUpdateMessage msg = (ConnectionUpdateMessage)message;
                    if (msg.Key != Key) {
                        return;
                    }
                    if (msg.Info != null) {
                        if (Info == null || Info.Timestamp < msg.Info.Timestamp) {
                            Info = msg.Info;
                        }
                    }
                    if (msg.Update != null) {
                        if (Update == null || Update.Timestamp < msg.Update.Timestamp) {
                            Update = msg.Update;
                        }
                    }
                    if (msg.Status != null) {
                        if (Status == null || Status.Timestamp < msg.Status.Timestamp) {
                            Status = msg.Status;
                        }
                    }
                    break;
            }
        }

        /// <summary>
        /// Gets the ConnectionKey.
        /// </summary>
        public ConnectionKey Key { get; private set; }

        /// <summary>
        /// This is the Info block for the connection. The Info contains information about the setup of the connection.
        /// </summary>
        public ConnectionInfo Info {
            get { return _Info; }
            private set {
                OnPropertyChanged("Info", ref _Info, value); 
            }
        }
        ConnectionInfo _Info;

        /// <summary>
        /// This is the Update block of the connection. It contains information about the operations the Connection
        /// does, which are mostly performance counters.
        /// </summary>
        public ConnectionUpdate Update {
            get { return _Update; }
            private set {
                OnPropertyChanged("Update", ref _Update, value);
            }
        }
        ConnectionUpdate _Update;

        /// <summary>
        /// This is the status block of the connection. It contains the current operational status in coded form as well as
        /// an accompanying text.
        /// </summary>
        public ConnectionStatus Status {
            get { return _Status; }
            private set {
                OnPropertyChanged("Status", ref _Status, value);
            }
        }
        ConnectionStatus _Status;

    }
}
